/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JPanel;
import vical.controller.EntryController;
import vical.model.WeekModel;

/**
 * The viewPanel is used to place and display a certain currentView.
 * It should manage which currentView (month / week / day ...) to display.
 * 
 * @author Can
 */
public class ViewPanel extends JPanel implements IView {

    public enum ViewType {

        MonthView, WeekView, DayView, ListView
    }
    private ViewType viewType;
    private AbstractView currentView;
    public MainWindow mainWindow;
    private NavigationBar navigationBar;
    private AbstractView[] views;

    private ViewPanel() {
    }

    public Component getNavigationBar() {
        return navigationBar;
    }

    public ViewPanel(MainWindow mainWindow) {
        this.mainWindow = mainWindow;
        // the default currentView is MonthView
        this.viewType = ViewType.MonthView;
        initViews();
        initComponents();
    }

    public ViewPanel(MainWindow mainWindow, ViewType viewType) {
        this.mainWindow = mainWindow;
        this.viewType = viewType;
        initViews();
        initComponents();
    }

    private void initViews() {
        views = new AbstractView[4];
        views[0] = new MonthView(this);
        views[1] = new WeekView(this, new WeekModel(EntryController.LoadEntries()));
        views[2] = new DayView();
        views[3] = new ListView();
    }

    private void initComponents() {
        navigationBar = new NavigationBar(this);
        currentView = this.getView(viewType);

        this.setLayout(new BorderLayout());
        this.add(BorderLayout.NORTH, navigationBar);
        this.add(BorderLayout.CENTER, currentView);

        updateCaption();
        navigationBar.setButtonPressed(viewType);
    }

    private void updateView() {
        if (currentView != null) {
            this.remove(currentView);
        }
        currentView = this.getView(this.viewType);
        updateCaption();
        navigationBar.setButtonPressed(viewType);
        this.add(BorderLayout.CENTER, currentView);
        this.currentView.refresh();
    }

    private AbstractView getView(ViewType type) {
        if (type == ViewType.MonthView) {
            return views[0];
        } else if (type == ViewType.WeekView) {
            return views[1];
        } else if (type == ViewType.DayView) {
            return views[2];
        } else if (type == ViewType.ListView) {
            return views[3];
        }
        return null;
    }

    public void setViewType(ViewType type) {
        this.viewType = type;
        updateView();
    }

    public void next() {
        this.currentView.next();
        updateCaption();
    }

    public void previous() {
        this.currentView.previous();
        updateCaption();
    }

    public void today() {
        this.currentView.today();
        updateCaption();
    }

    public void refresh() {
        this.currentView.refresh();
    }

    public void updateCaption() {
        navigationBar.setCaption(this.currentView.getCaption());
    }

    public void jumpToDate(Calendar cal) {
        this.currentView.jumpToDate(cal);
        updateCaption();
    }

    public void refreshLeftPanel() {
        mainWindow.updateLeftPanel();
    }

    public void highlightEntriesByCategory(String category) {
        this.currentView.highlightEntriesByCategory(category);
    }

    public void showEntriesByCategories(List<String> categories) {
        this.currentView.showEntriesByCategories(categories);
    }
    
    public void showEntriesByPriorities(ArrayList<String> priorities) {
        this.currentView.showEntriesByPriorities(priorities);
    }
}
